import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:playtruly/common/utils/toast_util.dart';

import 'index.dart';

class ActivteController extends GetxController {
  ActivteController();

  final state = ActivteState();
  
  // 活动类别选项
  final List<String> categories = ['吃', '喝', '玩', '乐', '购'];

  // 加载模拟数据
  void loadMockData() {
    state.isLoading.value = true;
    
    // 模拟网络延迟
    Future.delayed(const Duration(milliseconds: 500), () {
      // 生成模拟活动数据
      final activities = List.generate(12, (index) {
        final type = _getActivityType(index);
        final category = categories[index % categories.length];
        
        return ActivityItem(
          id: 'activity_$index',
          title: '$type #$index',
          date: '2025-07-03 18:32:00---2025-8-3 18:32',
          location: '城市体育中心',
          description: '附近的一起打球',
          participants: 5,
          imageUrl: '',  // 使用默认图片
          fullAddress: '河北省保定市容城县金容北塔149号番近容城体育馆',
          category: category,
          maxParticipants: 10,
          cost: '我买单',
          gender: '女',
        );
      });
      
      // 更新状态
      state.activities.assignAll(activities);
      state.isLoading.value = false;
    });
  }
  
  // 获取活动类型
  String _getActivityType(int index) {
    final types = ['台球搓子', '羽毛球搓子', '足球搓子', '篮球搓子', '游泳搓子'];
    return types[index % types.length];
  }

  // 处理活动点击，显示底部弹出框
  void onActivityTap(ActivityItem activity) {
    // 设置选中的活动
    state.selectedActivity.value = activity;
    
    // 显示底部弹出框
    _showActivityDetailBottomSheet(activity);
  }
  
  // 显示活动详情底部弹出框
  void _showActivityDetailBottomSheet(ActivityItem activity) {
    Get.bottomSheet(
      isScrollControlled: true, // 允许内容滚动
      backgroundColor: Colors.white,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(16),
          topRight: Radius.circular(16),
        ),
      ),
      SizedBox(
        height: Get.height * 0.9, // 占据屏幕90%的高度
        child: Column(
          children: [
            // 顶部图片区域
            Stack(
              children: [
                // 活动图片
                Container(
                  height: Get.width * 0.7, // 宽高比约为1.4:1
                  width: double.infinity,
                  color: Colors.blue[100],
                  child: Center(
                    child: Icon(
                      Icons.image,
                      size: 80,
                      color: Colors.blue[300],
                    ),
                  ),
                ),
                // 返回按钮
                Positioned(
                  top: 16,
                  left: 16,
                  child: GestureDetector(
                    onTap: () => Get.back(),
                    child: Container(
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: Colors.black.withOpacity(0.5),
                        shape: BoxShape.circle,
                      ),
                      child: const Icon(Icons.close, color: Colors.white),
                    ),
                  ),
                ),
              ],
            ),
            // 活动详情内容
            Expanded(
              child: SingleChildScrollView(
                padding: const EdgeInsets.symmetric(horizontal: 16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 活动标题和举报按钮
                    Row(
                      children: [
                        Expanded(
                          child: Text(
                            activity.title,
                            style: const TextStyle(
                              fontSize: 24,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        Container(
                          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 3),
                          decoration: BoxDecoration(
                            color: Colors.red[400],
                            borderRadius: BorderRadius.circular(16),
                          ),
                          child: const Text(
                            "举报",
                            style: TextStyle(color: Colors.white, fontSize: 12),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 16),
                    // 地点信息
                    Row(
                      children: [
                        const Icon(Icons.location_on, size: 20, color: Colors.grey),
                        const SizedBox(width: 8),
                        Expanded(
                          child: Text(
                            activity.fullAddress,
                            style: const TextStyle(fontSize: 14, color: Colors.black87),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 8),
                    // 时间信息
                    Row(
                      children: [
                        const Icon(Icons.access_time, size: 20, color: Colors.grey),
                        const SizedBox(width: 8),
                        Expanded(
                          child: Text(
                            activity.date,
                            style: const TextStyle(fontSize: 14, color: Colors.black87),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 20),
                    // 活动详情标题
                    const Text(
                      "活动详情",
                      style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                    ),
                    const SizedBox(height: 12),
                    // 类别
                    Row(
                      children: [
                        const Text("类别:", style: TextStyle(fontSize: 14, color: Colors.black54)),
                        const SizedBox(width: 8),
                        Wrap(
                          spacing: 8,
                          children: categories.map((category) {
                            bool isSelected = category == activity.category;
                            return Container(
                              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
                              decoration: BoxDecoration(
                                color: isSelected ? Colors.red[400] : Colors.grey[300],
                                borderRadius: BorderRadius.circular(16),
                              ),
                              child: Text(
                                category,
                                style: TextStyle(
                                  color: isSelected ? Colors.white : Colors.black87,
                                  fontSize: 14,
                                ),
                              ),
                            );
                          }).toList(),
                        ),
                      ],
                    ),
                    const SizedBox(height: 12),
                    // 人数
                    Row(
                      children: [
                        const Text("人数:", style: TextStyle(fontSize: 14, color: Colors.black54)),
                        const SizedBox(width: 8),
                        Text("${activity.participants}人", style: const TextStyle(fontSize: 14)),
                      ],
                    ),
                    const SizedBox(height: 8),
                    // 费用
                    Row(
                      children: [
                        const Text("消费:", style: TextStyle(fontSize: 14, color: Colors.black54)),
                        const SizedBox(width: 8),
                        Text(activity.cost, style: const TextStyle(fontSize: 14)),
                      ],
                    ),
                    const SizedBox(height: 8),
                    // 性别
                    Row(
                      children: [
                        const Text("性别:", style: TextStyle(fontSize: 14, color: Colors.black54)),
                        const SizedBox(width: 8),
                        Text(activity.gender, style: const TextStyle(fontSize: 14)),
                      ],
                    ),
                    const SizedBox(height: 20),
                    // 详细描述标题
                    const Text(
                      "详细描述",
                      style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                    ),
                    const SizedBox(height: 8),
                    // 详细描述内容
                    Text(
                      activity.description,
                      style: const TextStyle(fontSize: 14, height: 1.5),
                    ),
                    const SizedBox(height: 100), // 底部留白，防止被按钮遮挡
                  ],
                ),
              ),
            ),
            // 底部报名按钮
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
              decoration: const BoxDecoration(
                color: Colors.white,
                boxShadow: [
                  BoxShadow(
                    color: Colors.black12,
                    blurRadius: 4,
                    offset: Offset(0, -2),
                  ),
                ],
              ),
              child: SafeArea(
                child: GestureDetector(
                  onTap: () {
                    Get.back();
                    ToastUtil.success("已报名参加活动!");
                  },
                  child: Container(
                    width: double.infinity,
                    padding: const EdgeInsets.symmetric(vertical: 14),
                    decoration: BoxDecoration(
                      color: Colors.red[400],
                      borderRadius: BorderRadius.circular(8),
                    ),
                    alignment: Alignment.center,
                    child: Text(
                      "我要加入(剩余${activity.maxParticipants - activity.participants}名)",
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    // 加载数据
    loadMockData();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
